import plotly.graph_objs as go
from models import Product,RepairRecord,MaintainRecord,InspectionRecord

def get_radar_figure(values):
    categories = ['维修次数', '保养次数', '巡检次数', '强检次数', '校正次数']
    if len(values) != len(categories):
        print(f"[雷达图调试] Values list length mismatch: Expected {len(categories)}, got {len(values)}")
        values = [0] * len(categories)
    max_value = max(max(values), 1)
    return go.Figure(
        data=[
            go.Scatterpolar(r=[values[0], 0], theta=[categories[0], categories[0]], fill='toself', name='维修次数'),
            go.Scatterpolar(r=[values[1], 0], theta=[categories[1], categories[1]], fill='toself', name='保养次数'),
            go.Scatterpolar(r=[values[2], 0], theta=[categories[2], categories[2]], fill='toself', name='巡检次数'),
            go.Scatterpolar(r=[values[3], 0], theta=[categories[3], categories[3]], fill='toself', name='强检次数'),
            go.Scatterpolar(r=[values[4], 0], theta=[categories[4], categories[4]], fill='toself', name='校正次数')
        ],
        layout=go.Layout(
            polar=dict(
                radialaxis=dict(visible=False, range=[0, max_value]),
                angularaxis=dict(
                    tickmode='array',
                    tickvals=categories,
                    type='category',
                    rotation=90,
                    direction='clockwise',
                    showline=True,
                    linewidth=2
                )
            ),
            showlegend=False,
            legend=dict(x=0.7, y=0.5, xanchor='left', yanchor='middle', orientation='v'),
            margin=dict(l=30, r=30, t=30, b=30),
            paper_bgcolor='#1E90FF',
            plot_bgcolor='#1E90FF'
        )
    )

def get_running_detail_by_id(device_id, startdate=None, enddate=None):
    # 维修记录查询
    repair_query = RepairRecord.query.filter_by(product_id=device_id, is_active=True)
    # 保养记录查询
    maintain_query = MaintainRecord.query.filter_by(product_id=device_id, is_active=True)
    # 巡检记录查询
    inspection_query = InspectionRecord.query.filter_by(product_id=device_id, is_active=True)

    if startdate and enddate:
        # 如果有日期范围，添加日期过滤条件
        repair_query = repair_query.filter(
            RepairRecord.actual_completion_date >= startdate,
            RepairRecord.actual_completion_date <= enddate
        )
        maintain_query = maintain_query.filter(
            MaintainRecord.actual_date >= startdate,
            MaintainRecord.actual_date <= enddate
        )
        inspection_query = inspection_query.filter(
            InspectionRecord.actual_date >= startdate,
            InspectionRecord.actual_date <= enddate
        )

    # 获取计数
    repair_count = repair_query.count()
    maintain_count = maintain_query.count()
    inspection_count = inspection_query.count()
    
    # 固定值
    strong_inspect_count = 0
    calibration_count = 0
    
    return repair_count, maintain_count, inspection_count, strong_inspect_count, calibration_count

def update_run_report_graph_1(repair_count, maintain_count, inspection_count, strong_inspect_count, calibration_count):
    print(f"[调试 update_run_report_graph_1] 维修次数: {repair_count}, 保养次数: {maintain_count}, 巡检次数: {inspection_count}, 强检次数: {strong_inspect_count}, 校正次数: {calibration_count}")
    values = [repair_count, maintain_count, inspection_count, strong_inspect_count, calibration_count]
    print(f"[调试 update_run_report_graph_1] values: {values}")
    return get_radar_figure(values)
    